<?php

class SubfolderController extends Zend_Controller_Action
{
	private $directoryUtil;
	private $emailUtil;
	
    public function init()
    {
        $this->directoryUtil = new SubfolderAccessManager_Util_Directory();
        $this->emailUtil = new SubfolderAccessManager_Util_Email();
    }

    public function indexAction()
    {
        $this->view->title = 'Subfolder Search';

        $masterPath = $this->directoryUtil->getMasterPath();
        $this->view->masterPath = $masterPath;

        $form = new Application_Form_SearchSubfolder();
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();

            if ($form->isValid($formData)) {
                if (isset($_POST['enable'])) {
                    $this->forward('enable');
                } else if (isset($_POST['disable'])) {
                    $this->forward('disable');
                    }
            } else {
                $this->view->formResponse = 'Sorry, there was a problem with your submission. Please check the following:';
                $form->populate($formData);
            }

        }
    }

    public function listAction()
    {
        $this->view->title = 'Subfolder List';

        $masterPath = $this->directoryUtil->getMasterPath();
        $rootDir = $this->directoryUtil->getRootDirectory();
        
        $this->view->masterPath = $masterPath;
        $this->view->subDir = str_replace($rootDir, '', $masterPath);

        $directories = glob($masterPath . "*", GLOB_ONLYDIR);

        $folderExceptions = $this->directoryUtil->getFolderExceptions();
        $filteredSubfolders = array();

        foreach ($directories as $dir) {
            if (!in_array($dir, $folderExceptions)) {
                $subfolder = new Application_Model_Subfolder();
                $subfolder->subfolderName = basename($dir);
                $subfolder->hasAccess = $this->directoryUtil->hasAccessToSubfolder($dir);
                array_push($filteredSubfolders, $subfolder);
            }
        }

        $this->view->subfolders = $filteredSubfolders;
    }

    public function disableAction()
    {
        $this->view->title = 'Disable access to subfolder';
        
        $subfolderName = $this->_getParam('name');
        $rootDir = $this->directoryUtil->getRootDirectory();
        $masterPath = $this->directoryUtil->getMasterPath();
        
        $folderExceptions = $this->directoryUtil->getFolderExceptions();

        $this->view->subfolder = $subfolderName;
        $this->view->subDir = str_replace($rootDir, '', $masterPath);

        if (is_null($subfolderName)) {
            $this->view->error = 'Missing parameter: subfolder name';
        } else if (!is_dir($masterPath . $subfolderName)) {
            $this->view->error = 'Subfolder "' . $subfolderName .'" does not exist';
        } else if (in_array($subfolderName, $folderExceptions)) {
            $this->view->error = 'Modification of access to subfolder "' . $subfolderName .'" is restricted';
        } else if(!$this->directoryUtil->hasAccessToSubfolder($masterPath .  $subfolderName)) {
            $this->view->error = 'Access to subfolder "' . $subfolderName .'" is already disabled';
        }else {
            $this->directoryUtil->disableAccessToSubfolder($masterPath, $subfolderName);
            $this->emailUtil->sendSubfolderAccessNotification($rootDir, $masterPath, $subfolderName, false);
        }
    }

    public function enableAction()
    {
        $this->view->title = 'Enable access to subfolder';
        
        $subfolderName = $this->_getParam('name');
        $rootDir = $this->directoryUtil->getRootDirectory();
        $masterPath = $this->directoryUtil->getMasterPath();
        
        $folderExceptions = $this->directoryUtil->getFolderExceptions();

        $this->view->subfolder = $subfolderName;
        $this->view->subDir = str_replace($rootDir, '', $masterPath);

        if (is_null($subfolderName)) {
            $this->view->error = 'Missing parameter: subfolder name';
        } else if (!is_dir($masterPath . $subfolderName)) {
            $this->view->error = 'Subfolder "' . $subfolderName .'" does not exist';
        } else if (in_array($subfolderName, $folderExceptions)) {
            $this->view->error = 'Modification of access to subfolder "' . $subfolderName .'" is restricted';
        } else {
            $this->directoryUtil->enableAccessToSubfolder($masterPath, $subfolderName);
            $this->emailUtil->sendSubfolderAccessNotification($rootDir, $masterPath, $subfolderName, true);
        }
    }
}







